package com.cxs.drawpointdemo.util;

import com.cxs.drawpointdemo.constant.Common;
import com.cxs.drawpointdemo.data.Question;
import com.cxs.drawpointdemo.point.MyPoint;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class ZSONUtils {


    private static final String JSON_FILE = "resources/rawfile/question.json";

    private ZSONUtils() {}

    /**
     * List 转 ZSONString
     * @param list
     * @return
     */
    public static String toString(List<MyPoint> list) {
        String zsonStr = ZSONObject.toZSONString(list);
        return zsonStr;
    }

    /**
     * ZSONString 转 List
     * @param str
     * @return
     */
    public static List<MyPoint> toList(String str) {
        List<MyPoint> myPoints = new ArrayList<>();
        ZSONArray zsonArray = ZSONArray.stringToZSONArray(str);
        for (int i = 0; i < zsonArray.size(); i++) {
            ZSONObject zsonObj = zsonArray.getZSONObject(i);
            float floatX = zsonObj.getFloat(Common.POS_X_KEY);
            float floatY = zsonObj.getFloat(Common.POS_Y_KEY);
            Boolean isLastPoint = zsonObj.getBooleanValue(Common.LAST_POINT_KEY);
            myPoints.add(new MyPoint(floatX, floatY, isLastPoint));
        }
        return myPoints;
    }

    public static Question getQuestion(Context context) {
        List<Question> list = getQuestionList(context);
        int size = list.size();
        int i = new Random().nextInt(size);
        return list.get(i);
    }

    /**
     * json转List
     * @param context
     * @return
     */
    public static List<Question> getQuestionList(Context context) {
        String jsonFile = getJsonFile(context, JSON_FILE);
        ZSONArray zsonArray = ZSONArray.stringToZSONArray(jsonFile);
        int size = zsonArray.size();
        List<Question> list = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            ZSONObject zsonObj = zsonArray.getZSONObject(i);
            String title = zsonObj.getString(Common.QUESTION_TITLE_KEY);
            String describe = zsonObj.getString(Common.QUESTION_DESCRIBE_KEY);
            String tip1 = zsonObj.getString(Common.QUESTION_FIRST_TIP_KEY);
            String tip2 = zsonObj.getString(Common.QUESTION_SECOND_TIP_KEY);
            list.add(new Question(title, describe, tip1, tip2));
        }
        return list;
    }

    /**
     * 读取json文件
     * @param context
     * @param f
     * @return
     */
    private static String getJsonFile(Context context, String f) {
        try {
            Resource resource = context.getResourceManager().getRawFileEntry(f).openRawFile();
            byte[] tmp = new byte[Common.NEWS_CONTENT_SIZE * Common.NEWS_CONTENT_SIZE];
            int reads = resource.read(tmp);
            if (reads != Common.EOF) {
                return new String(tmp, 0, reads, StandardCharsets.UTF_8);
            }
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        return "";
    }
}
